load ..\dataforproject.mat
% Runs 3-fold avg for adaboost running adaboost on 7 different runs of 
% brute force SVM and 14 different runs of the svm on the non-brute-force
% SVM (the 21 'weak learners').

% Generate Data
% For bruteforce:
% generate train data
tr_data = gen_data_brute_force(X1train, X2train);
tr_label = ytrain;

% genereate test data
te_data = gen_data_brute_force(X1test, X2test);
te_label = ones(size(te_data,1),1);

% normalize the data to [0,1]. Test is normalized according to
% train data.
[tr_data, norm_params] = norm_data_brute_force(tr_data);
[te_data, ~] = norm_data_brute_force(te_data, norm_params);


% For non-bruteforce
% for L1
norm_type = 'L1';
tr_data_l1 = gen_data(X1train, X2train,norm_type);
tr_label_l1 = ytrain;

te_data_l1 = gen_data(X1test, X2test,norm_type);
te_label_l1 = ones(size(te_data_l1,1),1);

% for hellinger
norm_type = 'hellinger';   
tr_data_hellinger = gen_data(X1train, X2train,norm_type);
tr_label_hellinger = ytrain;

te_data_hellinger = gen_data(X1test, X2test,norm_type);
te_label_hellinger = ones(size(te_data_hellinger,1),1); 

% normalize before svm 
%L1
[tr_data_l1, norm_params] = norm_data(tr_data_l1);
te_data_l1= norm_data(te_data_l1, norm_params);

[tr_data_hellinger, norm_params] = norm_data(tr_data_hellinger);
te_data_hellinger= norm_data(te_data_hellinger, norm_params);
        


% Train SVMs
% Polynomial Kernel of degree 4
C_param = 1000;
gamma_param = 0.01;
coef0_param = 1;
degree_param = 4;
kernel_param = 1; 
% Brute force
model = train_svm(tr_data, tr_label, te_data, te_label, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_1, ~, ~] = svmpredict(tr_label, tr_data, model, '');
[test_results_1, ~, ~] = svmpredict(te_label, te_data, model, '');

% L1 norm
model = train_svm(tr_data_l1, tr_label_l1, te_data_l1, te_label_l1, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_2, ~, ~] = svmpredict(tr_label_l1, tr_data_l1, model, '');
[test_results_2, ~, ~] = svmpredict(te_label_l1, te_data_l1, model, '');


model = train_svm(tr_data_hellinger, tr_label_hellinger, te_data_hellinger, te_label_hellinger, kernel_param, C_param, gamma_param, coef0_param, degree_param);      
[train_results_3, ~, ~] = svmpredict(tr_label_hellinger, tr_data_hellinger, model, '');
[test_results_3, ~, ~] = svmpredict(te_label_hellinger, te_data_hellinger, model, '');


% Radial Kernel 
C_param = 1000;
gamma_param = 0.01;
kernel_param = 2;

model = train_svm(tr_data, tr_label, te_data, te_label, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_4, ~, ~] = svmpredict(tr_label, tr_data, model, '');
[test_results_4, ~, ~] = svmpredict(te_label, te_data, model, '');

model = train_svm(tr_data_l1, tr_label_l1, te_data_l1, te_label_l1, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_5, ~, ~] = svmpredict(tr_label_l1, tr_data_l1, model, '');
[test_results_5, ~, ~] = svmpredict(te_label_l1, te_data_l1, model, '');


model = train_svm(tr_data_hellinger, tr_label_hellinger, te_data_hellinger, te_label_hellinger, kernel_param, C_param, gamma_param, coef0_param, degree_param);      
[train_results_6, ~, ~] = svmpredict(tr_label_hellinger, tr_data_hellinger, model, '');
[test_results_6, ~, ~] = svmpredict(te_label_hellinger, te_data_hellinger, model, '');  





%Linear Kernel 
kernel_param = 0;

model7 = train_svm(tr_data, tr_label, te_data, te_label, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_7, ~, ~] = svmpredict(tr_label, tr_data, model7, '');
[test_results_7, ~, ~] = svmpredict(te_label, te_data, model7, '');

model = train_svm(tr_data_l1, tr_label_l1, te_data_l1, te_label_l1, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_8, ~, ~] = svmpredict(tr_label_l1, tr_data_l1, model, '');
[test_results_8, ~, ~] = svmpredict(te_label_l1, te_data_l1, model, '');


model = train_svm(tr_data_hellinger, tr_label_hellinger, te_data_hellinger, te_label_hellinger, kernel_param, C_param, gamma_param, coef0_param, degree_param);      
[train_results_9, ~, ~] = svmpredict(tr_label_hellinger, tr_data_hellinger, model, '');
[test_results_9, ~, ~] = svmpredict(te_label_hellinger, te_data_hellinger, model, '');        




% Different polynomial Kernels
C_param = 100;
gamma_param = 0.1;
coef0_param = 0;
degree_param = 2;
kernel_param = 1; % Polynomial kernel

model = train_svm(tr_data, tr_label, te_data, te_label, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_10, ~, ~] = svmpredict(tr_label, tr_data, model, '');
[test_results_10, ~, ~] = svmpredict(te_label, te_data, model, '');

model = train_svm(tr_data_l1, tr_label_l1, te_data_l1, te_label_l1, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_11, ~, ~] = svmpredict(tr_label_l1, tr_data_l1, model, '');
[test_results_11, ~, ~] = svmpredict(te_label_l1, te_data_l1, model, '');


model = train_svm(tr_data_hellinger, tr_label_hellinger, te_data_hellinger, te_label_hellinger, kernel_param, C_param, gamma_param, coef0_param, degree_param);     
[train_results_12, ~, ~] = svmpredict(tr_label_hellinger, tr_data_hellinger, model, '');
[test_results_12, ~, ~] = svmpredict(te_label_hellinger, te_data_hellinger, model, '');   


C_param = 10;
gamma_param = 0.1;
coef0_param = 0;
degree_param = 3;
kernel_param = 1; % Polynomial kernel

 model = train_svm(tr_data, tr_label, te_data, te_label, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_13, ~, ~] = svmpredict(tr_label, tr_data, model, '');
[test_results_13, ~, ~] = svmpredict(te_label, te_data, model, '');

model = train_svm(tr_data_l1, tr_label_l1, te_data_l1, te_label_l1, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_14, ~, ~] = svmpredict(tr_label_l1, tr_data_l1, model, '');
[test_results_14, ~, ~] = svmpredict(te_label_l1, te_data_l1, model, '');


model = train_svm(tr_data_hellinger, tr_label_hellinger, te_data_hellinger, te_label_hellinger, kernel_param, C_param, gamma_param, coef0_param, degree_param);     
[train_results_15, ~, ~] = svmpredict(tr_label_hellinger, tr_data_hellinger, model, '');
[test_results_15, ~, ~] = svmpredict(te_label_hellinger, te_data_hellinger, model, '');   


C_param = 10000;
gamma_param = 0.01;
coef0_param = 0;
degree_param = 2;
kernel_param = 1; % Polynomial kernel

 model = train_svm(tr_data, tr_label, te_data, te_label, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_16, ~, ~] = svmpredict(tr_label, tr_data, model, '');
[test_results_16, ~, ~] = svmpredict(te_label, te_data, model, '');   

model = train_svm(tr_data_l1, tr_label_l1, te_data_l1, te_label_l1, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_17, ~, ~] = svmpredict(tr_label_l1, tr_data_l1, model, '');
[test_results_17, ~, ~] = svmpredict(te_label_l1, te_data_l1, model, '');


model = train_svm(tr_data_hellinger, tr_label_hellinger, te_data_hellinger, te_label_hellinger, kernel_param, C_param, gamma_param, coef0_param, degree_param);    
[train_results_18, ~, ~] = svmpredict(tr_label_hellinger, tr_data_hellinger, model, '');
[test_results_18, ~, ~] = svmpredict(te_label_hellinger, te_data_hellinger, model, '');   



C_param = 1;
gamma_param = 0.1;
coef0_param = 0;
degree_param = 2;
kernel_param = 1; % Polynomial kernel        
 model = train_svm(tr_data, tr_label, te_data, te_label, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_19, ~, ~] = svmpredict(tr_label, tr_data, model, '');
[test_results_19, ~, ~] = svmpredict(te_label, te_data, model, '');         

model = train_svm(tr_data_l1, tr_label_l1, te_data_l1, te_label_l1, kernel_param, C_param, gamma_param, coef0_param, degree_param);
[train_results_20, ~, ~] = svmpredict(tr_label_l1, tr_data_l1, model, '');
[test_results_20, ~, ~] = svmpredict(te_label_l1, te_data_l1, model, '');


model = train_svm(tr_data_hellinger, tr_label_hellinger, te_data_hellinger, te_label_hellinger, kernel_param, C_param, gamma_param, coef0_param, degree_param);      
[train_results_21, ~, ~] = svmpredict(tr_label_hellinger, tr_data_hellinger, model, '');
[test_results_21, ~, ~] = svmpredict(te_label_hellinger, te_data_hellinger, model, '');   



% Adaboost
% Save all weak learners together - pre-processing for adaboost
dataFeatures = [train_results_1 train_results_2 train_results_3 train_results_4 train_results_5 train_results_6 train_results_7 train_results_8 train_results_9 train_results_10 train_results_11 train_results_12 train_results_13 train_results_14 train_results_15 train_results_16 train_results_17 train_results_18 train_results_19 train_results_20 train_results_21];
dataclass = tr_label;
testdata = [test_results_1 test_results_2 test_results_3 test_results_4 test_results_5 test_results_6 test_results_7 test_results_8 test_results_9 test_results_10 test_results_11 test_results_12 test_results_13 test_results_14 test_results_15 test_results_16 test_results_17 test_results_18 test_results_19 test_results_20 test_results_21];  

% Run adaboost
[~,model]=adaboost('train',dataFeatures,dataclass,500);        
ytest=adaboost('apply',testdata,model);



save ytest.mat ytest